package org.blog.controller;


import org.blog.entity.User;
import org.blog.entity.vo.UserCenterVO;
import org.blog.service.UserService;
import org.blog.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.*;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("userCenter/{userId}")
    public ModelAndView userCenter(@PathVariable("userId") Integer userId) {
        ModelAndView modelAndView = new ModelAndView("/front/user");
        UserCenterVO userCenterVO = userService.userCenter(userId);
        modelAndView.addObject("userCenterVO", userCenterVO);
        return modelAndView;
    }

    @GetMapping("front/logout")
    @ResponseBody
    public ResultResponse logout(HttpServletRequest request) {
        System.out.println("logout");
        Object user = request.getSession().getAttribute("user");
        if (user == null) {
            return new ResultResponse(false, "用户未登录，无需登出");
        } else {
            request.getSession().removeAttribute("user");
            return new ResultResponse(true, "登出成功");
        }

    }


    @GetMapping("front/toLogin")
    public String toLogin() {
        return "front/login";
    }

    @GetMapping("front/toRegister")
    public String toRegister() {
        return "front/register";
    }


    @PostMapping("front/uploadFile")
    @ResponseBody
    public ResultResponse uploadFile(@PathParam("file") MultipartFile file, HttpServletRequest request) {
        String fileName = file.getOriginalFilename();
        System.out.println(request.getSession().getServletContext().getRealPath("") + "front\\images\\" + fileName);
        if(fileName.indexOf("\\") != -1){
            fileName = fileName.substring(fileName.lastIndexOf("\\"));
        }
        String filePath = "front/images";
        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdir();
        }
        try {

            File file1 = new File(request.getSession().getServletContext().getRealPath("") + "front\\images\\" + fileName);
            file.transferTo(file1);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultResponse(false,"上传失败");
        }
        return new ResultResponse(true, filePath + "/" + fileName);
    }


    @PostMapping("front/updateUser")
    @ResponseBody
    public ResultResponse updateUser(User user) {
        System.out.println(user.toString());

        boolean update = userService.update(user);
        if (update) {
            return new ResultResponse(true, "修改成功");
        } else {
            return new ResultResponse(false, "修改时报");
        }

    }


    @PostMapping("front/register")
    @ResponseBody
    public ResultResponse register( User user) {

        User user1 = userService.selectByUserName(user.getUserName());
        if (user1 != null) {
            return new ResultResponse(false, "用户已存在");
        } else {
            boolean result = userService.add(user);
            if (result) {
                return new ResultResponse(true, "注册成功");

            } else {
                return new ResultResponse(false, "系统错误");
            }
        }
    }

    @PostMapping("front/login")
    @ResponseBody
    public ResultResponse login(User user, HttpServletRequest request) {
        System.out.println("login");
        User user2 = userService.selectByUserName(user.getUserName());
        if (user2 == null) {
            return new ResultResponse(false, "检测到您还没注册，请注册");
        } else {
            String userPassword = user.getUserPassword();
            System.out.println(userPassword);
            System.out.println(user2.getUserPassword());
            if (!userPassword.equals(user2.getUserPassword())) {
                return new ResultResponse(false, "账户名或密码错误");
            } else {
                request.getSession().setAttribute("user", user2);
                return new ResultResponse(true, "登录成功");

            }

        }
    }



}
